package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("FillerController")
public class FillerController extends CmiEmaController {

	@RequestMapping("/listFillers")
	public ModelAndView listFillers(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = fillerPaginationService.updatePagination(request, pagination);
		Set<Filler> fillers = fillerPaginationService.getPaginationFillers(pagination);

		mav.addObject("fillers", fillers);
		mav.addObject("entityName", "filler");
		mav.addObject("pagination", pagination);
		mav.setViewName("filler/listFillers.jsp");
		return mav;
	}

	public String listFillers() {
		return "redirect:/listFillers";
	}

	@RequestMapping("/viewFiller")
	public ModelAndView viewFiller(@RequestParam Integer fillerId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("filler", fillerDAO.findFillerByPrimaryKey(fillerId));
		mav.setViewName("filler/viewFiller.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFillers")
	public ModelAndView confirmDeleteFillers(
			@RequestParam(value = "deleteFillersIds", required = true) Set<Integer> deleteFillersIds) {
		ModelAndView mav = new ModelAndView();

		Set<Filler> deleteFillers = fillerDAO.findFillersByIds(deleteFillersIds);
		Set<Filler> referencedFillers = fillerService.loadReferenceFillers(deleteFillers);
		boolean hasReferenced;
		if (referencedFillers.size() == 0) {
			mav.addObject("deleteEntitys", deleteFillers);
			hasReferenced = false;
		} else {
			deleteFillers.removeAll(referencedFillers);
			mav.addObject("referencedEntitys", referencedFillers);
			mav.addObject("deleteEntitys", deleteFillers);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Filler");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFillers");
		return mav;
	}

	@RequestMapping("/deleteFiller")
	public String deleteFiller(@RequestParam Integer fillerId) {
		Filler filler = fillerDAO.findFillerByPrimaryKey(fillerId);
		fillerService.deleteFiller(filler);
		return "forward:/listFillers";
	}

	@RequestMapping("/confirmDeleteFiller")
	public ModelAndView confirmDeleteFiller(@RequestParam Integer fillerId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("filler", fillerDAO.findFillerByPrimaryKey(fillerId));

		mav.setViewName("filler/deleteFiller.jsp");

		return mav;
	}

	@RequestMapping("/deleteFillers")
	public String deleteFillers(
			@RequestParam(value = "deleteFillersId", required = true) Set<Integer> deleteFillersId) {
		fillerService.deleteFillers(deleteFillersId);
		return "forward:/listFillers";
	}

	@RequestMapping("/editFiller")
	public ModelAndView editFiller(@RequestParam Integer fillerId) {

		ModelAndView mav = new ModelAndView();

		Filler filler = fillerDAO.findFillerByPrimaryKey(fillerId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("filler", filler);

		// 把所有的引用filler对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(fillerService.loadFillerObjectReferencedSet(filler));

		// 将编辑页面filler添加到mav中
		mav.setViewName("filler/editFiller.jsp");
		return mav;
	}

	@RequestMapping("/newFiller")
	public ModelAndView newFiller() {
		ModelAndView mav = new ModelAndView();
		Filler filler = new Filler();

		mav.addObject("filler", filler);

		// 把所有的引用filler对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(fillerService.loadFillerObjectReferencedSet(filler));

		mav.addObject("newFlag", true);
		mav.setViewName("filler/editFiller.jsp");

		return mav;
	}

	@RequestMapping("/saveFiller")
	public ModelAndView saveFiller(@ModelAttribute Filler filler) {

		ModelAndView mav = new ModelAndView();

		Fillertype fillertypeTemp = filler.getFillertype();
		Fillertype fillertype = fillertypeTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(fillertypeDAO.findFillertypeById(fillertypeTemp.getId()));

		Filler existingFiller = CmiHibernateProxyUtil
				.deproxy(fillerDAO.findFillerByPhysicalPrimaryKey(filler.getName(), fillertype.getId()));

		filler.setFillertype(fillertype);
		// 新建记录
		if (filler.getId() == null) {
			if (filler.equals(existingFiller)) {

				mav.addAllObjects(fillerService.loadFillerClassReferencedSet(filler));
				// mav = editContinue(mav, filler);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("filler/editFiller.jsp");
			} else {
				fillerService.saveFiller(filler);
				mav.setViewName("forward:/listFillers");
			}
		}
		// 编辑记录
		else {
			Filler originFiller = CmiHibernateProxyUtil.deproxy(fillerDAO.findFillerByPrimaryKey(filler.getId()));
			if (!filler.equals(originFiller) && filler.equals(existingFiller)) {
				mav.addAllObjects(fillerService.loadFillerClassReferencedSet(filler));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("filler/editFiller.jsp");
			} else {
				fillerService.saveFiller(filler);
				mav.setViewName("forward:/listFillers");
			}
		}
		return mav;
	}

	@RequestMapping("/fillerController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
